﻿
using Org.LyrcsoftFramework.Cloud.Extensions;
using Org.LyrcsoftFramework.Cloud.OpenFeign.Attributes;
using Org.LyrcsoftFramework.Cloud.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Threading.Tasks;

namespace Org.LyrcsoftFramework.Cloud.Feign
{
    public class HttpService : FeignAspectSupport
    {

        public async Task<T> ExecuteAsync<T>(List<object> originArgs, MethodInfo method)
        {
            var args = new List<object>();
            originArgs.ForEach(it => args.Add(it));

            var interfaceType = method.DeclaringType;
            if (interfaceType == null) throw new Exception(nameof(interfaceType));
            var feignClientAttribute = interfaceType.GetCustomAttribute<FeignClientAttribute>();
            if (feignClientAttribute == null)
            {
                throw new Exception(method.Name + " have not FeignClientAttribute");
            }
            return await base.BaseExecuteAsync<T>(method, args.ToArray());
        }

        public async Task ExecuteNoReturnAsync(List<object> originArgs, MethodInfo method)
        {
            await this.ExecuteAsync<HttpResponseMessage>(originArgs, method);
        }
    }
}